package com.firefightingiot.firefightingiot.web.service;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.model.DeviceBusiness;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.support.mybatisplus.QueryWrapper;
import com.firefightingiot.firefightingiot.support.utils.hikvision.HikvisionInterfaceCall;
import com.firefightingiot.firefightingiot.support.utils.requestutil.HttpRequestUtil;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @Author: YangYu
 * @ClassName: VideoStreamService
 * @Date: Created in 10:55 2022/5/27
 * @Description: 视频流服务
 */
@Service
public class VideoStreamService extends BaseService {



    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;

    /**
     * 获取视频直播流
     * @param jsonParam
     * @return
     */
    public RespEntity getLiveStream(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //查询设备详情
        DeviceBusiness deviceBusiness = deviceBusinessMapper.selectOne(new QueryWrapper<DeviceBusiness>().eq("number", jsonParam.getStr("number")));
        if (deviceBusiness == null || deviceBusiness.getProductKey() == null) {
            return RespEntity.fail("设备号不存在!");
        }
        //直播流
        String liveStream = "";
        if (deviceBusiness.getProductId().equals("6011") || deviceBusiness.getProductId().equals("6003") || deviceBusiness.getProductId().equals("6002")){
            String result = HikvisionInterfaceCall.GetCameraPreviewURL(jsonParam.getStr("number"),"/api/video/v2/cameras/previewURLs",jsonParam.getStr("protocol"));
            JSONObject jsonObject = JSONUtil.parseObj(result);
            //rtmp数据
            JSONObject data = jsonObject.getJSONObject("data");
            liveStream = data.getStr("url");
        }
        return RespEntity.success(liveStream);
    }
    /**
     * 获取视频回放流
     * @param jsonParam
     * @return
     */
    public RespEntity getPlaybackStream(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //查询设备详情
        DeviceBusiness deviceBusiness = deviceBusinessMapper.selectOne(new QueryWrapper<DeviceBusiness>().eq("number", jsonParam.getStr("number")));
        if (deviceBusiness == null || deviceBusiness.getProductKey() == null) {
            return RespEntity.fail("设备号不存在!");
        }
        //回放流
        String playbackStream = "";
        if (deviceBusiness.getProductId().equals("6011") || deviceBusiness.getProductId().equals("6003") || deviceBusiness.getProductId().equals("6002")){
            //转换后开始时间
            String bt = StringToIos8601(jsonParam.getStr("beginTime"));
            //转换后结束时间
            String et = StringToIos8601(jsonParam.getStr("endTime"));
            String result = HikvisionInterfaceCall.GetHKPlayBackURL(jsonParam.getStr("number"),"/api/video/v2/cameras/playbackURLs","rtmp",bt,et);
            JSONObject jsonObject = JSONUtil.parseObj(result);
            JSONObject data = jsonObject.getJSONObject("data");
            playbackStream = data.getStr("url");
        }

        return RespEntity.success(playbackStream);
    }
    /**
     * String类型的时间(yyyy-MM-dd HH:mm:ss)转换成IOS-8601格式
     * @param data
     * @return
     */
    public static String StringToIos8601(String data) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dt1 = LocalDateTime.parse(data, dateTimeFormatter);
        ZoneOffset offset = ZoneOffset.of("+08:00");
        OffsetDateTime date = OffsetDateTime.of(dt1, offset);
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        String datas = date.format(formatter2);
        return datas;
    }

}
